<?php
class Role extends Model{

	public function getrole() {
		$sql = "select * from ".$this->db->prefix."role";
		return $this->db->getAll($sql);
	}
	public function getonerole() {
		$sql = "select * from ".$this->db->prefix."role where role_id = '".intval($_GET['rid'])."'";
		return $this->db->getRow($sql);
	}
	public function getprivilegerole() {
		$sql = "select p_id from ".$this->db->prefix."privilegerole where role_id = '".intval($_GET['rid'])."'";
		$arr = $this->db->getAll($sql);
		$newarr = array();
		foreach($arr as $value){
			$newarr[]=$value['p_id'];
		}
		return $newarr;
	}
	public function addrole() {
		$table = $this->db->prefix.'role';
		foreach($_POST['role_name'] as $key=>$value){
			$value = trim($value);
			if(!empty($value)){
				$arr = array('role_name'  =>$value
				);
				$this->db->insert($table, $arr);
			}
		}
	}
	public function updaterole() {
		$table = $this->db->prefix.'role';
		$wherestr = "role_id='".intval($_POST['role_id'])."'";
		$arr = array('role_name'=>$_POST['role_name']
		);
		$this->db->update($table, $arr,$wherestr);
	}
	public function delrole() {
		$table = $this->db->prefix.'role';
		$this->db->delete($table, 'role_id',$_GET['rid']);
	}
	public function assignrole() {
		$table = $this->db->prefix.'privilegerole';
		$range = intval($_POST['role_id']);
		$this->db->delete($table, 'role_id',$range);
		if(isset($_POST['roledb'])){
			foreach($_POST['roledb'] as $value){
				$table = $this->db->prefix.'privilegerole';
				$arr = array('role_id'=>$range,
								     'p_id'   =>$value
				);
				$this->db->insert($table, $arr);
			}
		}
	}
}